package com.my.jli.webapi;

import com.my.jli.Service.MyFileService;
import com.my.jli.entity.MyFile;
import com.my.jli.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @ClassName MyFileRestController
 * @Description
 * @Author MaYu
 * @Date 2022/3/28 14:11
 * @Version 1.0
 **/
@RestController
@RequestMapping("/webapi/file")
public class MyFileRestController {

    private MyFileService myFileService;
    @Autowired
    public void setMyFileService(MyFileService myFileService) {
        this.myFileService = myFileService;
    }

    @GetMapping("/getAll")
    public List<MyFile> getAll(){
        return myFileService.findAll();
    }

    /**
     * 获取文件信息列表，带分页和查询
     * @param page 要查询的页数
     * @param size 一页的大小
     * @param isTeacher 是否是老师查询
     * @param filename 文件名称
     * @param type 文件类型
     * @return 查询后带分页的文件信息列表
     */
    @GetMapping("/getByPage")
    public PageUtil getByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                              @RequestParam(value = "size", defaultValue = "5") Integer size,
                              @RequestParam(value = "isTeacher", defaultValue = "false") boolean isTeacher,
                              String filename, String type){

        //ding定义排序方式
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<MyFile> myFilePage;

        if ((filename == null && type == null) || (filename.equals("") && type.equals(""))){
            Pageable pageable = PageRequest.of(page, size, sort);
            if (isTeacher){
                //如果是老师则查询所有文件信息
                myFilePage = myFileService.findAll(pageable);
            } else {
                //不是只查询过审文件
                myFilePage = myFileService.findByJurisdiction(1, pageable);
            }
        } else {
            Pageable pageable = PageRequest.of(page, size, sort);

            MyFile myFile = new MyFile();
            myFile.setFilename(filename);
            myFile.setType(type);

            if (!isTeacher){
                //不是老师则只查询已过审的文件
                myFile.setJurisdiction(1);
            }
            //设置匹配器
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("filename", ExampleMatcher.GenericPropertyMatchers.contains())
                    .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.contains());
            Example<MyFile> myFileExample = Example.of(myFile, matcher);

            myFilePage = myFileService.findAll(myFileExample, pageable);
        }

        PageUtil pageUtil = new PageUtil(myFilePage.getContent(), myFilePage.getTotalElements(), page);
        pageUtil.setTotalPage(size);

        return pageUtil;
    }

    @GetMapping("/getAllType")
    public Set<String> getAllType(){
        return myFileService.findAllType();
    }

    @DeleteMapping ("/delete")
    public void deleteById(Integer id){
        myFileService.deleteById(id);
    }

    @PostMapping("/changeJurisdiction")
    public void changeJurisdiction(Integer id){
        MyFile myFile = myFileService.getById(id);
        Integer jurisdiction = myFile.getJurisdiction();
        if (jurisdiction == 0){
            myFile.setJurisdiction(1);
        } else {
            myFile.setJurisdiction(0);
        }
        myFileService.update(myFile);
    }
}
